//
//  ESTMoreViewController.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/2.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "ESTMoreViewController.h"
#import "UIButton+WebCache.h"
#import "ESTSettingTableViewController.h"

@interface ESTMoreViewController ()<UITableViewDelegate, UITableViewDataSource>
/**
 *  TableView
 */
@property(nonatomic, strong) TYZTableView *tableView;
/**
 *  数据源
 */
@property(nonatomic, strong) NSArray<NSArray<NSArray<NSString *> *> *> *dataArray;
@end

@implementation ESTMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupHeaderView];
    [self setupTableView];
}
- (void)setupHeaderView {
    TYZView *headerView = [[TYZView alloc] init];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(240);
    }];
    TYZImageView *imageView = [[TYZImageView alloc] initWithImage:[UIImage imageNamed:@"more_top_bg"]];
    [headerView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headerView);
    }];
    
    TYZButton *imageButton = [[TYZButton alloc] init];
    [headerView addSubview:imageButton];
    [imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:UserInfo.EMP_INFO.firstObject.HEAD_PORTRAIT_URL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"placeholder_person"]];
    [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(headerView).offset(-20);
        make.centerX.equalTo(headerView);
        make.width.height.mas_equalTo(80);
    }];
    imageButton.layer.cornerRadius = 40;
    imageButton.layer.masksToBounds = YES;
    [imageButton addTarget:self action:@selector(didClickHeaderImageButton) forControlEvents:UIControlEventTouchUpInside];
    
    TYZLabel *nameLabel = [[TYZLabel alloc] init];
    nameLabel.text = [NSString stringWithFormat:@"%@（%@）", UserInfo.EMP_INFO.firstObject.EMPLOYEE_NAME, UserInfo.EMP_INFO.firstObject.SYSTEM_STATION_NAME];
    [headerView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageButton);
        make.top.equalTo(imageButton.mas_bottom).offset(10);
    }];
    nameLabel.textColor = TYZWhiteColor;
    nameLabel.font = TYZFont(CoreInfo.fontSize);
    
    TYZLabel *detailLabel = [[TYZLabel alloc] init];
    detailLabel.text = UserInfo.EMP_INFO.firstObject.PHONE_NUMBER;
    [headerView addSubview:detailLabel];
    [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(10);
    }];
    detailLabel.textColor = TYZWhiteColor;
    detailLabel.font = TYZFont(CoreInfo.fontSize * 0.8);
}
- (void)setupTableView {
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(240);
    }];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ESTMoreViewControllerCell"];
}
#pragma mark - actions
- (void)didClickHeaderImageButton {
    
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < 1) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
            [cell setPreservesSuperviewLayoutMargins:NO];
        }
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return Value_Height_PartPadding;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    TYZView *view = [[TYZView alloc] init];
    view.backgroundColor = ESTBackgroundColor;
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESTMoreViewControllerCell" forIndexPath:indexPath];
    NSArray *arr = self.dataArray[indexPath.section][indexPath.row];
    cell.imageView.image = [UIImage imageNamed:arr.firstObject];
    cell.textLabel.text = arr.lastObject;
    cell.textLabel.textColor = TYZBlackColor;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (indexPath.section == 1) {// 最后一行
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, kWindowWidth);
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        [self.navigationController pushViewController:[[ESTSettingTableViewController alloc] init] animated:YES];
    }
}
#pragma mark - setter & getter
- (TYZTableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[TYZTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (NSArray<NSArray<NSArray<NSString *> *> *> *)dataArray {
    if (_dataArray == nil) {
        _dataArray = @[
                       @[@[@"more_file_list", [@"文件列表" S]],
                         @[@"more_help_center_icon", [@"帮助中心" S]],
                         @[@"more_suggestback_icon", [@"意见反馈" S]]],
                       @[@[@"more_set_icon", [@"设置" S]]]
                       ];
    }
    return _dataArray;
}
@end
